//
//  ContactViewCell.m
//  Showing
//
//  Created by Ralf on 4/9/15.
//  Copyright (c) 2015 next-software. All rights reserved.
//

#import "ContactViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "AppDelegate.h"

@interface ContactViewCell ()
{
    AppDelegate *appDelegate;
}
@end

@implementation ContactViewCell

- (void)awakeFromNib
{
    appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

    UIImageView *ivAvatar = (UIImageView *) [self viewWithTag:10];
    ivAvatar.layer.borderWidth = 1;
    ivAvatar.layer.borderColor = [[UIColor blackColor] CGColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    ContactViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ITEM"];

    if (cell == nil)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ContactViewCell" owner:nil options:nil][0];
    }

    return cell;
}

- (void)setData:(NSDictionary *)data
{
    NSDictionary *contact = data[@"contact"];
    UILabel      *lAlias  = (UILabel *) [self viewWithTag:20];
    lAlias.text = contact[@"alias"];

    UILabel  *lMsgCount    = (UILabel *) [self viewWithTag:30];
    NSNumber *tempMsgCount = data[@"numberOfUnreadMessages"];
    lMsgCount.text = [tempMsgCount stringValue];

    UIImageView *ivAvatar = (UIImageView *) [self viewWithTag:10];
    ivAvatar.image = nil;

    if ([contact[@"thumbnailURL"] isEqual:[NSNull null]])
    {

        ivAvatar.image = [UIImage imageNamed:@"Avatar-Default.png"];
    }
    else
    {
        NSString *thumbnailURL = contact[@"thumbnailURL"];
        NSString *url          = [NSString stringWithFormat:@"%@/%@", appDelegate.parameterManager.server, thumbnailURL];
        [ivAvatar sd_setImageWithURL:[NSURL URLWithString:url]];
    }
}

@end
